<!-- 头部和尾部公共js -->

/* 登录 */
$(function () {
    function loginSuccess(){
        $.ajax({
            type: "post",
            url: "user/LoginSuccessServlet",
            success: function (data) {
                var $ul;
                if (data == "true") {
                    $ul = "<ul>\n" +
                        "<li><a href='account.html'>我的信息</a></li>\n" +
                        "<li><a href=''>我发布的宝贝</a></li>\n" +
                        "<li><a href=''>卖出的宝贝</a>\t</li>\n" +
                        "<li><a href=''>买到的宝贝</a></li>\n" +
                        "<li><a href='#' id='loginOut'>注销登录</a></li>\n" +
                        "</ul>";
                } else {
                    $ul = "<ul>\n" +
                        "<li class=\"login-icon\"><a href=\"login.html\" title=\"Login\"><i class=\"fa fa-user\"></i> 登录</a></li>\n" +
                        "<li class=\"register-icon\"><a href=\"register.html\" title=\"Register\"><i class=\"fa fa-user-plus\"></i> 注册</a></li>\n" +
                        "</ul>";
                }
                $("#content-dropdown").append($ul);
            }
        });
    }
    loginSuccess();


    $(".login-icon").on("click","#loginOut",function () {
        $.ajax({
            url:"/user/LoginOutServlet",
            type: "POST",
            dataType:"JSON",
            success:function (data) {
                if(data == true){
                    confirm("确认退出吗?");
                    /*退出登录成功*/
                    $("#content-dropdown").empty()
                   loginSuccess();
                }
            }
        })
    })
});
/*登录结束*/

/* ---头部购物车开始--- */
$(function () {
    /*请求购物车*/
    function getCart(){
        $.ajax({
            url: "/ShowCartServlet",
            type: "POST",
            dataType: "JSON",
            success: function (data) {
                $(".cart-list").empty();
                $("#cart-notification").html(data.length);
                if(data.length == 0){
                    $(".cart-list").append("<p class='text-center mt-2'>购物车里空空如也</p>");
                    $(".cart-tip").hide();
                    $(".cart-sub-totle").hide();
                    $(".checkout").addClass("disabled");
                }else{
                    var totalPrice = 0;
                    $(".cart-tip").show();
                    $(".cart-sub-totle").show();
                    $(".checkout").removeClass("disabled");
                    $.each(data,function (index, item) {
                        if(index < 5){
                            var li = "<li>\n" +
                                "        <div class=\"media\">\n" +
                                "            <a class=\"pull-left\"> <img alt=\"Shopholic\" src=\"goodsimage/"+item.goods.image+"\"></a>\n" +
                                "            <div class=\"media-body\"> " +
                                "                 <p><a href=\"#\">"+item.goods.goodsName+"</a></p>\n" +
                                "                 <span class=\"cart-price\">$"+Number(item.goods.price).toFixed(2)+"</span>\n" +
                                "                 <span class=\"buy-num ml-2\">数量:"+item.buyNum+"</span>\n" +
                                "            </div>\n" +
                                "        </div>\n" +
                                "      </li>";
                            $(".cart-list").append(li);
                        }
                        totalPrice += item.goods.price * item.buyNum;
                    });
                    if(data.length > 5){
                        $(".cart-tip").show();
                        $(".remain").html(data.length - 5);
                    }else{
                        $(".cart-tip").hide();
                    }
                    $("#priceBox").html(Number(totalPrice).toFixed(2));
                }

            }
        })
    }
    getCart();

    $("#cartIcon").mouseover(function () {
        getCart();
        $("#cartIcon").unbind("mouseover",getCart());
        $("#cartIcon").bind("mouseout",getCart());
    });
    $("#cartIcon").mouseout(function () {
        $("#cartIcon").bind("mouseover",getCart());
        $("#cartIcon").unbind("mouseout",getCart());
    });


    $("#cartLink").click(function () {
        getCart();
    });
});
/* ---头部购物车结束--- */